package com.diors.game.engine.arpg.npc;

import java.util.concurrent.atomic.AtomicLong;

import com.diors.game.engine.arpg.npc.entity.NpcEntity;
import com.diors.game.engine.arpg.npc.model.NpcModel;
import com.diors.game.engine.arpg.scene.entity.MapEntity;
import com.diors.game.engine.arpg.scene.entity.SceneEntity;
import com.diors.game.engine.arpg.scene.entity.SceneObject;

/**
 * NPC工厂类
 * @author	darren.ouyang <ouyang.darren@gmail.com>
 * @date	2013-10-26
 * @version 1.0
 * @copyright Copyright (c) 2013, darren.ouyang
 */
public class NpcFacotry 
{

	private final static AtomicLong atomicID = new AtomicLong(1L);
	
	/**
	 * npc生成
	 * @param adapter	NPC适配器
	 * @return
	 * @create	2013-10-26	darren.ouyang
	 */
	public static NpcEntity createNpc (NpcAdapter adapter)
	{
		adapter = initAdapter(adapter);
		if (adapter == null)
			return null;
			
		NpcModel model = adapter.model;
		
		NpcEntity npc = new NpcEntity();
		createNpcID(adapter, npc);
		npc.setBaseID(model.getId());
		createNpcSceneObject(adapter, npc);
		return npc;
	}
	
	private static NpcAdapter initAdapter (NpcAdapter adapter)
	{
		if (adapter.model == null)
			return null;
		
		if (adapter.scene == null)
			return null;
		
		if (adapter.line == null)
			return null;
		
		return adapter;
	}
	
	private static void createNpcID (NpcAdapter adapter, NpcEntity npc)
	{
		npc.setId(atomicID.incrementAndGet());
	}
	
	private static void createNpcSceneObject (NpcAdapter adapter, NpcEntity npc)
	{
		SceneObject sceneObject = new SceneObject();

		npc.setSceneObject(sceneObject);
	}
	
	/**
	 * NPC适配器
	 * @author	darren.ouyang <ouyang.darren@gmail.com>
	 * @date	2013-10-26
	 * @version 1.0
	 * @copyright Copyright (c) 2013, darren.ouyang
	 */
	static class NpcAdapter
	{
		NpcModel model;
		
		int x;
		
		int y;
		
		int d;
		
		SceneEntity scene;
		
		MapEntity line;
		
		int lineID;

		public void setModel(NpcModel model) {
			this.model = model;
		}

		public void setX(int x) {
			this.x = x;
		}

		public void setY(int y) {
			this.y = y;
		}

		public void setD(int d) {
			this.d = d;
		}

		public void setScene(SceneEntity scene) {
			this.scene = scene;
		}

		public void setLineID(int lineID) {
			this.lineID = lineID;
		}

		public void setLine(MapEntity line) {
			this.line = line;
		}
		
		@Override
		public String toString() 
		{
			StringBuilder sb = new StringBuilder("npcID:");
			
			if (model==null)
				sb.append("null");
			else
				sb.append(model.getId());
			
			sb.append(", xyd[").append(x).append(",").append(y).append(",").append(d).append("], sceneID:");
			/*if (scene==null)
				sb.append("null");
			else
				sb.append(scene.getID());*/
			
			return sb.toString();
		}
	}
}
